package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.domain.PaymentSlips;
import com.ruoyi.finance.domain.PaymentVoucher;
import com.ruoyi.finance.domain.PaymentWaterFormUpload;
import com.ruoyi.finance.domain.vo.PaymentMasterVo;
import com.ruoyi.finance.domain.vo.PaymentVoucherLogVo;
import com.ruoyi.finance.domain.vo.PaymentVoucherVo;
import com.ruoyi.finance.service.IPaymentVoucherService;
import com.ruoyi.procured.domain.ProcuredPlan;
import com.ruoyi.procured.domain.vo.PaymentRequestLogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 付款单Controller
 *
 */
@RestController
@RequestMapping("/finance/PaymentVoucher")
public class PaymentVoucherController extends BaseController
{
    @Autowired
    private IPaymentVoucherService paymentVoucherService;

    /**
     * 查询付款单列表
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:list')")
    @GetMapping("/selectPaymentVoucherList")
    public TableDataInfo list(PaymentMasterVo paymentMasterVo)
    {
        List<PaymentMasterVo> list = paymentVoucherService.selectPaymentMasterVotList(paymentMasterVo);
        return getDataTable(list);
    }

    /**
     * 核准付款单
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:approved')")
    @PostMapping("/approvedPaymentVoucher/{paymentVoucherIds}")
    public ResponseEntity<String> approvedPaymentVoucher(@PathVariable List<String> paymentVoucherIds) {

        if(paymentVoucherIds.isEmpty()){
            throw new ServiceException("获取付款单ID失败。");
        }

        paymentVoucherService.approvedPaymentVoucher(paymentVoucherIds,getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }
    /**
     * 批量核准（单独付款）
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:approved')")
    @PostMapping("/approvedPaymentVoucherOnly/{paymentVoucherIds}")
    public ResponseEntity<String> approvedPaymentVoucherOnly(@PathVariable List<String> paymentVoucherIds) {

        if(paymentVoucherIds.isEmpty()){
            throw new ServiceException("获取付款单ID失败。");
        }

        paymentVoucherService.approvedPaymentVoucherOnly(paymentVoucherIds,getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }
    /**
     * 查询日志
     */
    @GetMapping("/selectPaymentVoucherLogList")
    public TableDataInfo selectPaymentVoucherLogList(PaymentVoucherLogVo paymentVoucherLog){
        startPage();
        List<PaymentVoucherLogVo> paymentVoucherLogs = paymentVoucherService.selectPaymentVoucherLogList(paymentVoucherLog);
        return getDataTable(paymentVoucherLogs);
    }
    /**
     * 驳回付款单
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:reject')")
    @PostMapping("/rejectPaymentVoucher")
    public ResponseEntity<String> rejectPaymentVoucher(@RequestParam List<String> paymentVoucherIds, @RequestParam String rejectRemark) {

        if(paymentVoucherIds.isEmpty()){
            throw new ServiceException("获取付款单ID失败。");
        }
        if(StringUtils.isNull(rejectRemark)){
            throw new ServiceException("驳回原因不能为空。");
        }

        paymentVoucherService.rejectPaymentVoucher(paymentVoucherIds,rejectRemark,getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }
    /**
     * 上传水单
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:posting')")
    @PostMapping("/uploadWaterBillImg")
    public AjaxResult edit(@RequestBody PaymentWaterFormUpload formUpload)
    {
        return toAjax(paymentVoucherService.uploadWaterBillImg(formUpload,getNickname()));
    }
    /**
     * 撤回采购计划
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:revoke')")
    @PostMapping("/revokeProcuredPlan")
    public AjaxResult revokeProcuredPlan(@RequestBody ProcuredPlan procuredPlan, @RequestParam("componentPath") String componentPath)
    {
        if(procuredPlan.getPlanId() == null){
            throw new ServiceException("采购计划ID为空。");
        }
        if(procuredPlan.getRevokeRemark() == null){
            throw new ServiceException("撤回备注不能为空。");
        }
        return toAjax(paymentVoucherService.revokeProcuredPlan(procuredPlan,getNickname(),getUserId(),componentPath));
    }
    /**
     * 锁定付款
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentVoucher:taglock')")
    @PostMapping("/tagLockPaymentVoucher/{paymentVoucherIds}")
    public ResponseEntity<String> tagLockPaymentVoucher(@PathVariable List<String> paymentVoucherIds) {

        if(paymentVoucherIds.isEmpty()){
            throw new ServiceException("获取付款单ID失败。");
        }

        paymentVoucherService.tagLockPaymentVoucher(paymentVoucherIds,getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }

}
